#coding=utf-8
import MySQLdb
import uuid
from  datetime  import  *
from collections import namedtuple



conn= MySQLdb.connect(
        host='localhost',
        port = 3306,
        user='root',
        passwd='richie',
        db ='contract_product_2017-8-28',
        charset="utf8"
        )
cur = conn.cursor()

#创建数据表
#cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))")

#插入一条数据
#cur.execute("insert into student values('2','Tom','3 year 2 class','9')")


reCount = cur.execute("select * from yx_receipt_detail")
# result = cur.fetchall()


#修改查询条件的数据
# cur.execute("update base_car_brand set logo='3 year 1 class' where ID = '2c95802b55b592240155b5aebb3b0eb1'")

#删除查询条件的数据
#cur.execute("delete from student where age='9'")

def generate_namedtuple(cur):
    from collections import namedtuple
    fieldnames = [d[0].lower() for d in cur.description]
    Record = namedtuple('Record', fieldnames)
    rows = cur.fetchall()
    if not rows:return
    else:
        return map(Record._make, rows)

for k in generate_namedtuple(cur):
        origin_id = k.id
        trace_id = uuid.uuid1()
        trace_id = str(trace_id).replace("-","")

        apply_id = uuid.uuid1()
        apply_id = str(apply_id).replace("-","")

        todo_task_id  = uuid.uuid1()
        todo_task_id = str(todo_task_id).replace("-","")

        now = datetime.now()
        str_now = now.strftime('%Y-%m-%d %H:%M:%S')
        #   新增表（yx_receipt_detail_trace）数据
        cur.execute(
                "insert into yx_receipt_detail_trace( ID,BZ_ID,CREATE_TIME,CREATOR_DEPARTMENT_ID,CREATOR_DEPARTMENT_NAME,CREATOR_ID,CREATOR_NAME,IS_DELETED,UPDATE_TIME,UPDATOR_DEPARTMENT_ID,UPDATOR_DEPARTMENT_NAME,UPDATOR_ID,UPDATOR_NAME,VERSION,apply_invoice_amount,invoice_remark,receipt_amount,receipt_code,receipt_desc,receipt_no,receipt_sort,receivable_remark,contract_id,receipt_id )"
                " values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s"            
                " )", (
                trace_id,k.bz_id,
                k.create_time.strftime("%Y-%m-%d %H:%M:%S") if not (k.create_time is None)   else '',
                k.creator_department_id,k.creator_department_name,
                k.creator_id,
                k.creator_name,
                k.is_deleted,
                k.update_time,
                k.updator_department_id,
                k.updator_department_name,
                k.updator_id,
                k.updator_name,k.version,
                k.apply_invoice_amount, k.invoice_remark, k.receipt_amount, k.receipt_code, k.receipt_desc, k.receipt_no,
                k.receipt_sort, k.receivable_remark, k.contract_id, k.receipt_id))

        print( apply_id, k.bz_id,k.create_time,k.creator_department_id,k.creator_department_name,k.creator_id,k.creator_name,k.is_deleted,k.update_time,k.updator_department_id,k.updator_department_name,k.updator_id,k.updator_name,k.version,k.id ,'','','' )

cur.close()
conn.commit()
conn.close()
# for r in result:
        # print r
        # 写入trace 表
        # print (r['bill_number'],r['bill_type'],r['checker'])
        # print (r['bill_number'])
        # 写入 apply表
